﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MockupDesigner.Items.Metadatas;

namespace MockupDesigner.AppItems.Controls
{
    /// <summary>
    /// Interaction logic for MetadataControl.xaml
    /// </summary>
    public partial class MetadataControl : UserControl
    {
        public MetadataControl()
        {
            InitializeComponent();
        }

        public void SetMetadatable(IMetadatable metadatable)
        {
            this.DataContext = metadatable;
        }

        private void Add_Click(object sender, RoutedEventArgs e)
        {
            if (this.DataContext == null)
                return;
            IList<Metadata> list = ((IMetadatable)this.DataContext).Metadatas != null ? ((IMetadatable)this.DataContext).Metadatas.ToList() : new List<Metadata>(1);
            list.Add(new Metadata() { Order = list.Count });
            ((IMetadatable)this.DataContext).Metadatas = list.ToArray();
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            IList<Metadata> list = ((IMetadatable)this.DataContext).Metadatas.ToList();
            list.RemoveAt((int)((Button)sender).DataContext);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Order = i;
            }
            ((IMetadatable)this.DataContext).Metadatas = list.ToArray();
        }

        private void Up_Click(object sender, RoutedEventArgs e)
        {
            int index = (int)((Button)sender).DataContext;
            if (index == 0)
                return;
            IList<Metadata> list = ((IMetadatable)this.DataContext).Metadatas.ToList();
            list[index].Order -= 1;
            list[index - 1].Order += 1;
            ((IMetadatable)this.DataContext).Metadatas = list.OrderBy(m => m.Order).ToArray();
        }

        private void Down_Click(object sender, RoutedEventArgs e)
        {
            int index = (int)((Button)sender).DataContext;
            IList<Metadata> list = ((IMetadatable)this.DataContext).Metadatas.ToList();
            if (index == list.Count - 1)
                return;
            list[index].Order += 1;
            list[index + 1].Order -= 1;
            ((IMetadatable)this.DataContext).Metadatas = list.OrderBy(m => m.Order).ToArray();
        }


        private void KeyTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (this.DataContext == null)
                return;
            TextBox tb = (TextBox)e.Source;
            if (((IMetadatable)this.DataContext).Metadatas.Count(md => md.Key == tb.Text) > 1)
            {
                tb.Text = tb.Text + "1";
            }
        }

    }
}
